package com.hspedu.homework;

import java.util.Scanner;

/**
 * @Author Stringzhua
 * @Date 2024/3/10 15:42
 * description:
 */
public class homework02 {
    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        System.out.print("请输入用户名:");
//        String username = scanner.next();
//        System.out.print("请输入密码:");
//        String password = scanner.next();
//        System.out.print("请输入邮箱号:");
//        String email = scanner.next();

        String username = "jack";
        String password = null;
        String email = "2785649457@qq.com";

        try {
            Userregister(username, password, email);
            System.out.println("恭喜！注册成功!");
            System.out.println("请妥善保存以下信息:" + "\n用户名:" + username + "\n密码:" + password);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void Userregister(String username, String password, String email) {
        //加入校验
        if (!(username != null && password != null && email != null)) {
            throw new RuntimeException("参数不能为空");
        }


        //(1)用户名长度为2~4
        int Userlength = username.length();
        if (!(Userlength >= 2 && Userlength <= 4)) {
            throw new RuntimeException("用户名长度为2~4");
        }
        //(2)密码的长度为6,要求全是数字
        //这里要单独的写一个方法,判断密码是否全部为数字字符
        if (!(password.length() == 6 && isDigital(password))) {
            throw new RuntimeException("密码的长度为6,要求全是数字");
        }
        //(3)邮箱中包含@和. 并且@在.的前面 isDigital
        int i = email.indexOf("@");
        int j = email.indexOf(".");
        if (!(i > -1 && i < j)) {
            throw new RuntimeException("邮箱中包含@和. 并且@在.的前面");
        }
    }

    //这里要单独的写一个方法,判断密码是否全部为数字字符
    public static boolean isDigital(String str) {
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] < '0' || chars[i] > '9') {
                //说明不是数字
                return false;
            }
        }
        return true;
    }

}
